﻿@model BMCShowView.Portal.ViewModels.BackendViewModel
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>

    <style>
        label {
            display: block;
        }
    </style>
</head>
<body>
    <h1>武汉大屏后端</h1>
    <form method="post" id="RemarkForm">
        <fieldset>
            <legend>值班备注项</legend>
            <label>
                <span>内容：</span>
                <input class="content" type="text" name="remark" maxlength="88" value="@Model.Remark.Content" />
            </label>
            <label>
                <span>更新时间：</span>
                <input class="time" type="text" value="@Model.Remark.CreateTime.ToString()" readonly="readonly" />
            </label>
            <div><input class="submit" type="button" value="提交" /> <input class="clear" type="button" value="清空" /></div>
        </fieldset>
    </form>

    <form method="post" id="AnnouncementForm">
        <fieldset>
            <legend>通知公告</legend>
            <label>
                <span>内容：</span>
                <textarea class="content" name="announcement" rows="16" cols="120">@Model.Announcement.Content</textarea>
            </label>
            <label>
                <span>更新时间：</span>
                <input class="time" type="text" value="@Model.Announcement.CreateTime.ToString()" readonly="readonly" />
            </label>
            <div><input class="submit" type="button" value="提交" /> <input class="clear" type="button" value="清空" /></div>
        </fieldset>
    </form>

    <script src="~/Scripts/fetch.js"></script>
    <script src="~/Scripts/Backend.js"></script>
    <script>
        Array.prototype.forEach.call(document.querySelectorAll("form"), function (form) {
            var title = form.querySelector("legend").textContent;
            form.querySelector("input.submit").addEventListener("click", function () {
                if (confirm("确定提交" + title + "吗？")) {
                    fetch("/Backend", {
                        method: 'post',
                        body: new FormData(form)
                    }).then(function (response) {
                        return response.json();
                    }).then(function (data) {
                        alert(title + "已经更新");
                        form.querySelector("input.time").value = new Date(Number(data.CreateTime.replace("/Date(", "").replace(")/", ""))).toLocaleString();
                    });
                }
            });
            form.querySelector("input.clear").addEventListener("click", function () {
                if (confirm("确定清空" + title + "吗？")) {
                    form.querySelector(".content").value = "";
                }
            });
        });
    </script>
</body>
</html>
